#include <string>
#include "echo.h"
#include "native_delegate.h"

#ifdef __cplusplus
extern "C" {
#endif

native_delegate delegate;

jstring str(JNIEnv *env,
            jclass) {

    std::string hello = "Hello from C++ 动态注册";

    return env->NewStringUTF(hello.c_str());
}

jint init(JNIEnv *env, jclass, jobject context) {
    return delegate.init(env, context);
}

jstring sign(JNIEnv *env,
             jclass,
             jstring str
) {

    return delegate.sign(env, str);
}


jint JNI_OnLoad(JavaVM *vm, void *) {

    JNIEnv *env = nullptr;
    //获取JniEnv
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return JNI_EVERSION;
    }

    //获取一个class对象,对应的java类
    jclass clazz = env->FindClass("top/icsat/android/echo/AndroidNative");
    if (clazz == nullptr) {
        return JNI_ERR;
    }

    JNINativeMethod methods[] = {
            // 方法映射
            {"sign",   "(Ljava/lang/String;)Ljava/lang/String;", (jstring *) sign},
            {"navStr", "()Ljava/lang/String;",                   (jstring *) str},
            {"init",   "(Landroid/content/Context;)I",           (jint *) init},
    };

    if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) != JNI_OK) {
        return JNI_ERR;
    }

    return JNI_VERSION_1_6;
}

#ifdef __cplusplus
}
#endif